use Qlbanhang

--Procedures Mua Hang
-----------Proc lay danh sach Mua Hang

GO
if(OBJECT_ID('DanhSach_MuaHang','p') is not null)
begin
	drop proc  DanhSach_MuaHang
end
GO
CREATE PROCEDURE DanhSach_MuaHang 
	@soTrang int
AS
BEGIN
	select * from (
	select top ((
				Select count(*) from (SELECT top (22*@soTrang) *
				FROM NhapHang)d
				)-(22*(@soTrang-1)))
	*from (		
	select top (22*@soTrang) *
	from NhapHang order by NhapHang_ID DESC
	) a	order by NhapHang_ID ASC
	) b	order by NhapHang_ID DESC
		
END
GO
-----------Proc Select so luong Mua Hang
if(OBJECT_ID('SoLuong_MuaHang','p') is not null)
begin
	drop proc  SoLuong_MuaHang
end
GO
CREATE PROCEDURE SoLuong_MuaHang 
AS
BEGIN
	Select count(*) FROM NhapHang
END

GO
--Procedures Ban Hang
-----------Proc lay danh sach Ban Hang

GO
if(OBJECT_ID('DanhSach_XuatHang','p') is not null)
begin
	drop proc  DanhSach_XuatHang
end
GO
CREATE PROCEDURE DanhSach_XuatHang
	@soTrang int
AS
BEGIN
	select * from (
	select top ((
				Select count(*) from (SELECT top (22*@soTrang) *
				FROM XuatHang)d
				)-(22*(@soTrang-1)))
	*from (		
	select top (22*@soTrang) *
	from XuatHang order by XuatHang_ID DESC
	) a	order by XuatHang_ID ASC
	) b	order by XuatHang_ID DESC
		
END
GO
-----------Proc Select so luong Ban Hang
if(OBJECT_ID('SoLuong_XuatHang','p') is not null)
begin
	drop proc  SoLuong_XuatHang
end
GO
CREATE PROCEDURE SoLuong_XuatHang
AS
BEGIN
	Select count(*) FROM XuatHang
END

GO

--Procedures Thu Tien
-----------Proc lay danh sach Thu Tien
GO
if(OBJECT_ID('DanhSach_KetThucGDBanHang','p') is not null)
begin
	drop proc  DanhSach_KetThucGDBanHang
end
GO
CREATE PROCEDURE DanhSach_KetThucGDBanHang
	@soTrang int
AS
BEGIN
	select * from (
	select top ((
				Select count(*) from (SELECT top (22*@soTrang) *
				FROM KetThucGDBanHang)d
				)-(22*(@soTrang-1)))
	*from (		
	select top (22*@soTrang) *
	from KetThucGDBanHang order by ID_GDBanHang DESC
	) a	order by ID_GDBanHang ASC
	) b	order by ID_GDBanHang DESC
		
END
GO
-----------Proc Select so luong Thu Tien
if(OBJECT_ID('SoLuong_KetThucGDBanHang','p') is not null)
begin
	drop proc  SoLuong_KetThucGDBanHang
end
GO
CREATE PROCEDURE SoLuong_KetThucGDBanHang
AS
BEGIN
	Select count(*) FROM KetThucGDBanHang
END

GO

--Procedures Tra Tien
-----------Proc lay danh sach Tra Tien
GO
if(OBJECT_ID('DanhSach_KetThucGDMuaHang','p') is not null)
begin
	drop proc  DanhSach_KetThucGDMuaHang
end
GO
CREATE PROCEDURE DanhSach_KetThucGDMuaHang
	@soTrang int
AS
BEGIN
	select * from (
	select top ((
				Select count(*) from (SELECT top (22*@soTrang) *
				FROM KetThucGDMuaHang)d
				)-(22*(@soTrang-1)))
	*from (		
	select top (22*@soTrang) *
	from KetThucGDMuaHang order by ID_GDMuaHang DESC
	) a	order by ID_GDMuaHang ASC
	) b	order by ID_GDMuaHang DESC
		
END
GO
-----------Proc Select so luong Tra Tien
if(OBJECT_ID('SoLuong_KetThucGDMuaHang','p') is not null)
begin
	drop proc  SoLuong_KetThucGDMuaHang
end
GO
CREATE PROCEDURE SoLuong_KetThucGDMuaHang
AS
BEGIN
	Select count(*) FROM KetThucGDMuaHang
END

GO


--Procedures Hang Hoa
-----------Proc lay danh sach HangHoa
GO
if(OBJECT_ID('DanhSach_HangHoa','p') is not null)
begin
	drop proc  DanhSach_HangHoa
end
GO
CREATE PROCEDURE DanhSach_HangHoa
	@soTrang int
AS
BEGIN
	select * from (
	select top ((
				Select count(*) from (SELECT top (22*@soTrang) *
				FROM HangHoa)d
				)-(22*(@soTrang-1)))
	*from (		
	select top (22*@soTrang) *
	from HangHoa join DonVi on HangHoa.DonViSP = DonVi.MaDV 
	join DiaChiKho on HangHoa.Kho = DiaChiKho.MaK 
	join NhomHang on HangHoa.NhomSP = NhomHang.MaNH order by MaSP DESC
	) a	order by MaSP ASC
	) b	order by MaSP DESC
		
END
GO
-----------Proc Select so luong HangHoa
if(OBJECT_ID('SoLuong_HangHoa','p') is not null)
begin
	drop proc  SoLuong_HangHoa
end
GO
CREATE PROCEDURE SoLuong_HangHoa
AS
BEGIN
	Select count(*) FROM HangHoa
END

GO